package com.hontech.bvapp2.ui.parts.cargo

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.Button
import android.widget.FrameLayout
import com.hontech.bvapp2.R
import com.hontech.bvapp2.device.cp.Cargo
import com.hontech.bvapp2.ui.dialog.runProg
import com.hontech.bvapp2.ui.view.LabelInput
import com.hontech.bvapp2.ui.view.LabelSpinner
import com.hontech.bvapp2.utils.onClick

class CargoPickCtrlParts (context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_cargo_pick_ctrl, this)
        val attr = context.obtainStyledAttributes(attrs, R.styleable.CargoPickCtrlParts)
        val cargo = attr.getInteger(R.styleable.CargoPickCtrlParts_pick_ctrl_cargo, 0)
        attr.recycle()

        val btn = view.findViewById<Button>(R.id.id_cargo_pick_ctrl_btn)
        val li = view.findViewById<LabelInput>(R.id.id_cargo_pick_ctrl_li_position)

        val sp = view.findViewById<LabelSpinner>(R.id.id_cargo_pick_sp_cols)
        val btnCol = view.findViewById<Button>(R.id.id_cargo_pick_col_btn)

        btnCol.onClick {
            val col = sp.selectIndex() + 1
            runProg("取货单价", "控制$col") {
                val position = Cargo.cargoPositionWithCol(col)
                Cargo.pickCtrl(cargo, position)
            }
        }

        btn.onClick {
            runProg("取货电机", "控制") {
                val position = li.textInt()
                Cargo.pickCtrl(cargo, position)
            }
        }
    }
}